import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author wq3stone
 */
public class Page2Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        try {
            int num2 = Integer.parseInt(req.getParameter("num"));
            conn = DBUtil.getConnection();
            String sql = "update tb_sum set num=? where id=2";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,num2);
            resp.getWriter().print("您输入的数字为："+num2);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            resp.getWriter().print("请不要输入非数字类型的字符");
        } finally {
            DBUtil.close(conn,ps,null);
        }
    }
}
